package com.letter.otools.day.util;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import java.util.List;

public class StringUtils {
    /**
     * 高亮字符串
     *
     * @param textView
     * @param content
     * @param highLightText
     * @param color
     */
    public static void setColorSpan(TextView textView, String content, String highLightText, int color) {
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        int START_AG = content.indexOf(highLightText);
        int END_AG = START_AG + highLightText.length();
        SpannableString spannableString = new SpannableString(content);
        spannableString.setSpan(colorSpan, START_AG, END_AG, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableString);
    }

    //拼接字符串
    public static String join(String regex, List<String> params) {
        if (params == null || params.isEmpty()) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < params.size(); i++) {
            String s = params.get(i);
            if (!TextUtils.isEmpty(s)) {
                sb.append(s);
                if (i != params.size() - 1 && !TextUtils.isEmpty(regex)) {
                    sb.append(regex);
                }
            }
        }
        String ret = "";
        if (sb.length() > 0) {
            ret = sb.toString();
        }
        return ret;
    }

    //拼接字符串
    public static String join(String regex, String... params) {
        StringBuffer sb = new StringBuffer();
        for (String s : params) {
            if (!TextUtils.isEmpty(s)) {
                sb.append(s);
                if (!TextUtils.isEmpty(regex)) {
                    sb.append(regex);
                }
            }
        }
        String ret = "";
        if (sb.length() > 0) {
            ret = sb.deleteCharAt(sb.length() - (!TextUtils.isEmpty(regex) ? regex.length() : 0)).toString();
        }
        return ret;
    }

    public static boolean isEmpty(String s){
        return TextUtils.isEmpty(s);
    }

    public static boolean isNotEmpty(String s){
        return !TextUtils.isEmpty(s);
    }
}
